package com.xtm.exercise.base.B01session;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/***
    springSession 共享session , 底层就是将session保存到reids中
    分别启动
    先访问： localhost:20001/springSession/set
    后访问： localhost:20001/springSession/get
 */
@RestController
@RequestMapping("/springSession")
public class SpringSessionController {

    /**
     * 获取项目端口
     */
    @Value("${server.port}")
    private String port;

    /**
     * 将信息存放在session中
     * localhost:20001/springSession/set
     */
    @GetMapping("/set")
    public String set(HttpSession session) {
        session.setAttribute("user", "hello world~~~");
        return port;
    }

    /**
     * 从session中获取信息
     * localhost:20001/springSession/get
     */
    @GetMapping("/get")
    public String get(HttpSession session) {
        return session.getAttribute("user") + " : " + port;
    }


}
